package com.yanqu.road.dao.impl.activity.snowgarden;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.snowgarden.config.SnowGardenLevelConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SnowGardenLevelConfigDaoImpl extends TempDao {

    private static String getSnowGardenLevelConfigSql = "select * from `t_s_activity_snowgarden_level` where `activityId`=?";

    public Map<Integer, SnowGardenLevelConfig> getSnowGardenLevelConfig(int activityId){
        Map<Integer, SnowGardenLevelConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSnowGardenLevelConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SnowGardenLevelConfig snowGardenLevelConfig = new SnowGardenLevelConfig();
                snowGardenLevelConfig.setActivityId(rs.getInt("activityId"));
                int id = rs.getInt("id");
                snowGardenLevelConfig.setId(id);
                snowGardenLevelConfig.setLevel(rs.getInt("level"));
                snowGardenLevelConfig.setGain(rs.getInt("gain"));
                map.put(id, snowGardenLevelConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
